/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.InvalidArgumentException;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.TooManyHopsException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class MaxForwardsHeaderImpl extends HeaderBase implements MaxForwardsHeader {
    private int maxForwards;

    private static final MaxForwardsHeaderParser parser = new MaxForwardsHeaderParser();

    public MaxForwardsHeaderImpl() {
        super(MaxForwardsHeader.NAME);
    }

    MaxForwardsHeaderImpl(byte[] data, int start, int end) {
        super(MaxForwardsHeader.NAME, data, start, end);
    }

    public Object clone() {
        final MaxForwardsHeaderImpl header = new MaxForwardsHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.maxForwards = this.maxForwards;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        return Integer.toString(this.maxForwards);
    }

    /**
     * This convenience function decrements the number of max-forwards by one.
     * <p/>
     * This utility is useful for proxy functionality.
     *
     * @throws javax.sip.header.TooManyHopsException
     *          if implementation cannot decrement
     *          max-fowards i.e. max-forwards has reached zero
     */

    public void decrementMaxForwards() throws TooManyHopsException {
        parse();
        this.maxForwards--;
        invalidateHeaderData();
    }

    /**
     * Gets the maximum number of forwards value of this MaxForwardsHeader.
     *
     * @return the maximum number of forwards of this MaxForwardsHeader
     */

    public int getMaxForwards() {
        parse();
        return this.maxForwards;
    }

    /**
     * Sets the max-forwards argument of this MaxForwardsHeader to the supplied
     * <p/>
     * <var>maxForwards</var> value.
     *
     * @param maxForwards the number of max-forwards
     * @throws javax.sip.InvalidArgumentException
     *          if the maxForwards argument is less
     *          than 0 or greater than 255.
     */

    public void setMaxForwards(int maxForwards) throws InvalidArgumentException {
        parse();
        this.maxForwards = maxForwards;
        invalidateHeaderData();
    }
}
/*
 * $Log$
 */